<?php
///////////////////////////////////////////////////////////////////////////////////////////////////////
//  这个文件是 JCAT PHP框架的一部，该项目和此文件 均遵循 GNU 自由软件协议
// 
//  Copyleft 2008 JeCat.cn(http://team.JeCat.cn)
//
//
//  JCAT PHP框架 的正式全名是：Jellicle Cat PHP Framework。
//  “Jellicle Cat”出自 Andrew Lloyd Webber的音乐剧《猫》（《Prologue:Jellicle Songs for Jellicle Cats》）。
//  JCAT 是一个开源项目，它像音乐剧中的猫一样自由，你可以毫无顾忌地使用JCAT PHP框架。JCAT 由中国团队开发维护。
//  正在使用的这个版本是：0.5.0 / SVN信息: $Id: class.JCAT_UICtrlHtmlBase.php 1594 2009-03-31 11:29:38Z alee $
//
//
//
//  相关的链接：
//    [主页] http://jcat.JeCat.cn
//    [下载(HTTP)] http://code.google.com/p/jcat-php/downloads/list
//    [下载(svn)] svn checkout http://jcat-php.googlecode.com/svn/branches/0.4.0/Framework/ JCAT0.4
//    [在线文档] http://jcat.JeCat.cn/document
//    [社区] http://jj.jecat.cn/forum-7-1.html
//  不很相关：
//    [MP3] http://www.google.com/search?q=jellicle+songs+for+jellicle+cats+Andrew+Lloyd+Webber
//    [VCD/DVD] http://www.google.com/search?q=CAT+Andrew+Lloyd+Webber+video
//
///////////////////////////////////////////////////////////////////////////////////////////////////////
/*-- Project Introduce --*/
namespace JCAT ;



/**
 * What's this Class ?
 *
 * @author		alee
 * @access		public
 */
abstract class UICtrlHtmlBase extends UICtrlBase
		implements IUICtrlHtml, IJSObject, IHTMLResourceContainer
{

	public function UICtrlHtmlBase( $sName, $sUIName='' )
	{
		$this->SetName($sName,$sUIName) ;
		$this->SetHtmlId($sName.'Id') ;
	}

	/**
	 * 取得 JS UI控件对象的变量名
	 *
	 * @access	public
	 * @param	
	 * @return	void
	 */
	public function GetJSVariableName()
	{
		return 'theUICtrl' . $this->GetName() ;
	}
	
	/**
	 * What's this Method ?
	 * 
	 * @access	public
	 * @param	$sHtmlId	string	What's this Parameter ?
	 * @return	string
	 */
	public function SetHtmlId( $sHtmlId )
	{
		ASSERT::ASSERT_STRING($sHtmlId) ;
		return $this->sHtmlId = $sHtmlId ;
	}
	
	/**
	 * What's this Method ?
	 * 
	 * @access	public
	 * @return	string
	 */
	public function GetHtmlId()
	{
		return $this->sHtmlId ;
	}



	/**
	 * What's this Method ?
	 * 
	 * @access	public
	 * @param	$sName	string	What's this Parameter ?
	 * @param	$sValue	string	What's this Parameter ?
	 * @return	old_var
	 */
	public function SetStyle( $sName, $sValue )
	{
		ASSERT::ASSERT_STRING($sName) ;
		ASSERT::ASSERT_STRING($sValue) ;
		
		$old_value = isset($this->arrStyles[$sName])? $this->arrStyles[$sName]: null ;
		$this->arrStyles[$sName] = $sValue ;
		return $old_value ;
	}



	/**
	 * What's this Method ?
	 * 
	 * @access	public
	 * @param	$sName	string	What's this Parameter ?
	 * @return	string, null
	 */
	public function GetStyle( $sName ) 
	{
		ASSERT::ASSERT_STRING($sName) ;
		return isset($this->arrStyles[$sName])? $this->arrStyles[$sName]: null ;
	}



	/**
	 * What's this Method ?
	 * 
	 * @access	public
	 * @return	string
	 */
	public function GetStyleStream()
	{
		$arrStream = array() ;
		foreach($this->arrStyles as $sStyleName=>$sStyleCode)
			$arrStream[] = "{$sStyleName}=\"{$sStyleCode}\"" ;
		
		return implode(' ',$arrStream) ;
	}



	/**
	 * What's this Method ?
	 * 
	 * @access	public
	 * @param	$sName		What's this Parameter ?
	 * @return	string
	 */
	public function RemoveStyle(  $sName )
	{
		$old_value = isset($this->arrStyles[$sName])? $this->arrStyles[$sName]: null ;
		unset($this->arrStyles[$sName]) ;
		return $old_value ;		
	}



	/**
	 * What's this Method ?
	 * 
	 * @access	public
	 * @param	$sName	string	What's this Parameter ?
	 * @param	$sValue	string	What's this Parameter ?
	 * @return	old_var
	 */
	public function SetAttribute( $sName, $sValue )
	{
		ASSERT::ASSERT_STRING($sName) ;
		ASSERT::ASSERT_STRING($sValue) ;
		
		$old_value = isset($this->arrAttributes[$sName])? $this->arrAttributes[$sName]: null ;
		$this->arrAttributes[$sName] = $sValue ;
		return $old_value ;
	}




	/**
	 * What's this Method ?
	 * 
	 * @access	public
	 * @param	$sName	string	What's this Parameter ?
	 * @return	string
	 */
	public function GetAttribute( $sName )
	{
		ASSERT::ASSERT_STRING($sName) ;
		return isset($this->arrAttributes[$sName])? $this->arrAttributes[$sName]: null ;
	}




	/**
	 * What's this Method ?
	 * 
	 * @access	public
	 * @param	$bIncludeStyle=true	bool	What's this Parameter ?
	 * @return	string
	 */
	public function GetAttributeStream( $bIncludeStyle=true )
	{
	
		$arrStream = array() ;
		foreach($this->arrAttributes as $sAttrName=>$sAttrCode)
		{
			if( !$bIncludeStyle and strtolower($sAttrName)=='style' )
				continue ;
			$arrStream[] = "{$sAttrName}=\"{$sAttrCode}\"" ;
		}
		
		return implode(' ',$arrStream) ;
	}



	/**
	 * What's this Method ?
	 * 
	 * @access	public
	 * @param	$sName		What's this Parameter ?
	 * @return	string
	 */
	public function RemoveAttribute(  $sName )
	{
		$old_value = isset($this->arrAttributes[$sName])? $this->arrAttributes[$sName]: null ;
		unset($this->arrAttributes[$sName]) ;
		return $old_value ;		
	}



	/**
	 * What's this Method ?
	 * 
	 * @access	public
	 * @param	$aUI	UIHtml	What's this Parameter ?
	 * @return	void
	 */
	public function Display( UIHtml $aUI )
	{
		ASSERT::ASSERT_FILE($this->sMainUITemplate,Language::SentenceEx('UICtrlHtml：“%s”尚未设置 有效的模版文件。%s','JCAT',null,get_class($this),$this->sMainUITemplate)) ;
		
		$aOldVars = $aUI->CreateVarSetMemento() ;
		$aUI->SetVar('TheUICtrl',$this) ;
		
		$aUI->Display($this->sMainUITemplate) ;
		
		$aUI->SetVarSetMemento($aOldVars) ;
	}
	
	
	/**
	 * 
	 *
	 * @access	public
	 * @param	$sTemplate	string		What's this Parameter ?
	 * @return	void
	 */
	public function SetUITemplate( $sTemplate )
	{
		ASSERT::ASSERT_STRING($sTemplate) ;
		$this->sMainUITemplate = $sTemplate ;
	}
	
	/**
	 * create a iterator for html resources
	 *
	 * @access	public
	 * @param	$bRecursion=false		bool		if this object has child object and $bRecursion=true, iterator will iterate all child's
	 * @return	IIterator
	 */
	public function CreateHTMLResourceIterator($bRecursion=false)
	{
		return new ArrayIterator($this->arrHTMLResources) ;
	}
	
	
	/**
	 * add a html resource, only call this method inner
	 *
	 * @access	protected
	 * @param	$bRecursion=false		bool
	 * @return	void
	 */
	protected function AddHTMLResource(IHTMLResource $aHtmlResource)
	{
		if( !in_array($aHtmlResource,$this->arrHTMLResources,true) )
		{
			$this->arrHTMLResources[] = $aHtmlResource ;
		}
	}
	
	

	// 属性 ///////////////////////////////////////////////////////////////////////////////



	private $sHtmlId ;
	
	
	private $arrStyles = array() ;
	
	
	private $arrAttributes = array() ;
	
	
	protected $sMainUITemplate ;
	
	private $arrHTMLResources = array() ;

}
?>